// ---------------------------------------------------------------------------------------------------------------------------------
// Core Control File for PCMod
// [CLIENTSIDE]
// Revision: 2
// ---------------------------------------------------------------------------------------------------------------------------------

// ---------------------------------------------------------------------------------------------------------------------------------
// Define core table
// ---------------------------------------------------------------------------------------------------------------------------------
PCMod = {}
PCMod.Version = "1.0.2"
PCMod.Exists = true
PCMod.WireEnabled = 0

Msg( "===> Loading PCMod Core <===\n" )
Msg( "===> PCMod Clientside OS (Version " .. PCMod.Version .. ") Initialized <===\n" )


// ---------------------------------------------------------------------------------------------------------------------------------
// Define important globals
// ---------------------------------------------------------------------------------------------------------------------------------
w = ScrW()
h = ScrH()

// Our current PC information
C_PC = {}
C_PC.Status = 0
C_PC.Online = false
C_PC.IP = ""
C_PC.PList = {}
C_PC.SysTray = {}
C_PC.DST = {}
C_PC.DIcons = {}
C_PC.Files = {}
CurrentPCIndex = 0
ScreenTexID = 0

PCLoaded = false

CurrentProgram = ""
CurrentPC = nil

PC_ScreenSizeX = 0
PC_ScreenSizeY = 0

PC_IGrid_H = 5
PC_IGrid_W = 5

CurrentTheme = {}
PCLIB = {}

// ---------------------------------------------------------------------------------------------------------------------------------
// Read out required convars, and act upon them
// ---------------------------------------------------------------------------------------------------------------------------------
ShouldDrawFrame = true
CV_SDF = CreateClientConVar( "pc_shoulddrawframe", 1, true, false )
CV_NOTICE = CreateClientConVar( "pc_hidenotices", 0, true, false )
if (CV_SDF:GetInt() == 0) then ShouldDrawFrame = false end

CV_FRAME = CreateClientConVar( "pc_frameid", 1, true, false )
FrameID = CV_FRAME:GetInt()
FrameTex = surface.GetTextureID( "vgui/pc_screen" .. tostring(FrameID) )
function pc_loadframe( id )
	FrameTex = surface.GetTextureID( "vgui/pc_screen" .. tostring(id) )
end
pc_loadframe(FrameID)

// ---------------------------------------------------------------------------------------------------------------------------------
// Define low level functions
// ---------------------------------------------------------------------------------------------------------------------------------
function PCMod:Print( txt )
	Msg( "PCMod -> " )
	Msg( txt )
	Msg( "\n" )
end

function PCMod:ReportError( txt )
	self:Print( "ERROR: " .. txt )
end

function PCMod:ReportWarning( txt )
	self:Print( "WARNING: " .. txt )
end

// ---------------------------------------------------------------------------------------------------------------------------------
// Define important tables
// ---------------------------------------------------------------------------------------------------------------------------------
Texs = {} -- Texture Storage
C_PRO = {} -- Current Program Storage
ProgramData = {} -- Program Data
LoadedIcons = {} -- Icon Texture IDs
DesktopIcons = {} -- Desktop Icons


// ---------------------------------------------------------------------------------------------------------------------------------
// Load all libraries
// ---------------------------------------------------------------------------------------------------------------------------------
for k, v in pairs( file.FindInLua( "pclibs/*.lua" ) ) do
	include( "pclibs/" .. v )
end


// ---------------------------------------------------------------------------------------------------------------------------------
// Include important files
// ---------------------------------------------------------------------------------------------------------------------------------

include( "cl_programs.lua" ) -- Program Loading
include( "cl_themes.lua" ) -- Theme Loading
include( "pcmodcore/cl_vgui.lua" ) -- VGUI Control
include( "pcmodcore/cl_dmenu.lua" ) -- DInterface Library
include( "pcmodcore/cl_pcscreen.lua" ) -- PC Screen Control
include( "pcbeamvars/cl_pcbeamvars.lua" ) -- PC BeamVars Library
include( "pcmodcore/cl_letter.lua" ) -- Letter Control
include( "pcmodcore/cl_desktopicons.lua" ) -- Desktop Icon Control
include( "cl_serverapps.lua" ) -- Server Apps Control

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_init: Event Hook: Initialises all the core stuff
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_init()
	// Create our basic fonts
	pc_initfonts()
	
	// Setup the 'settings' window
	pc_initsettings()
	
	// Setup the computer screen gui
	pc_setupscreen()
	pc_setupletter()
end
hook.Add( "Initialize", "PCMod_Initialize", pc_init )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_wireaddon: UserMessage Hook: Reads whether wire is installed or not (OBSOLETE)
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_wireaddon( um )
	PCMod.WireEnabled = um:ReadShort()
	if (PCMod.WireEnabled == 0) then	
		PCMod:Print( "WireMod NOT detected!" )
	else
		PCMod:Print( "WireMod detected!" )
	end
end
usermessage.Hook( "_haswire", pc_wireaddon )

// ---------------------------------------------------------------------------------------------------------------------------------
// StatusToString: Converts a numerical status to a string status
// ---------------------------------------------------------------------------------------------------------------------------------
function StatusToString( status )
	if (status == 0) then return "Off" end
	if (status == 1) then return "Booting" end
	if (status == 2) then return "Login" end
	if (status == 3) then return "On" end
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_showoptions: ConCommand Hook: Shows the 'settings' window
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_showoptions()
	DInterface:Show()
end
concommand.Add( "pcmod_show", pc_showoptions )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_showoptions: ConCommand Hook: Hides the 'settings' window
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_hideoptions()
	DInterface:Hide()
end
concommand.Add( "pcmod_hide", pc_hideoptions )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_setstatus: Sets the numerical status of the locally stored variable
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_setstatus( status )
	C_PC.Status = status
end

// ---------------------------------------------------------------------------------------------------------------------------------
// SendCommand: Sends a con command in the form of 'pc_command prog_command <command> <args>'
// ---------------------------------------------------------------------------------------------------------------------------------
function SendCommand( command, ... )
	RunConsoleCommand( "pc_command", "prog_command", command, ... )
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_refresh: Think Hook: Reads the current PC's data into local variable
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_refresh()
	if (!C_PRO) then C_PRO = {} end
	if (!CurrentPC) or (CurrentPCIndex == 0) then return end
	C_PC.Status = PCBeam:GetBeamValue( CurrentPC, "Status", 0 )
	C_PC.Online = PCBeam:GetBeamValue( CurrentPC, "Online", false )
	C_PC.IP = PCBeam:GetBeamValue( CurrentPC, "IP", "" )
	local dicons = DInterface:GetPanel( "dicons" )
	// if (dicons) then
	if (C_PC.Status == 3) then pc_showdesktopicons() end
	if (C_PC.Status != 3) then pc_hidedesktopicons() end
	// end
	if (C_PC.Status != 2) then
		local txtPass = DInterface:GetObject( "txtPass", "tb", PTC("desktop") )
		local btnGo = DInterface:GetObject( "btnGo", "b", PTC("desktop") )
		if (txtPass) then DInterface:RemoveTextbox( "txtPass", PTC("desktop") ) end
		if (btnGo) then DInterface:RemoveButton( "btnGo", PTC("desktop") ) end
	end
	if (C_PC.Status != 3) then
		local btnGo = DInterface:GetObject( "btnStart", "b", PTC("desktop") )
		if (btnGo) then DInterface:RemoveButton( "btnStart", PTC("desktop") ) end
		if (C_PRO.Exists) then pc_progunload() end
	end
end
hook.Add( "Think", "PCMod_Think", pc_refresh )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_progtools: Called when the program tool-menu is opened
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_progtools()
	PCMod:Print( "Tools clicked on!" )
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_command: Sends a con command in the form: 'pc_command <command>'
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_command( command )
	RunConsoleCommand( "pc_command", command )
end

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_themeupdate: UserMessage Hook: Updates the theme upon notification of the server
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_themeupdate( um )
	local th = um:ReadString()
	CurrentTheme = ThemeList[ th ]
	if (!CurrentTheme) then CurrentTheme = ThemeList[ "DefaultBlue" ] end
end
usermessage.Hook( "_pcmod_themeupdate", pc_themeupdate )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_beamlist: UserMessage Hook: Updates the current program list
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_beamlist( um )
	if (!C_PC) then return end
	C_PC.PList = {}
	cnt = um:ReadShort()
	for i=1,cnt do
		C_PC.PList[i] = um:ReadString()
	end
	pc_builddesktopicons()
end
usermessage.Hook( "_pcmod_beamlist", pc_beamlist )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_beamdata_str: Usermessage Hook: Pushes data through to the program
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_beamdata_str( um )
	if (!C_PRO) then C_PRO = {} end
	if (!C_PRO.Exists) then return end
	C_PRO:DataRecieved( PTC("window"), { um:ReadString(), um:ReadString() } )
end
usermessage.Hook( "_pcmod_beamdata_str", pc_beamdata_str )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_beamdata_int: Usermessage Hook: Pushes data through to the program
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_beamdata_int( um )
	if (!C_PRO) then C_PRO = {} end
	if (!C_PRO.Exists) then return end
	C_PRO:DataRecieved( PTC("window"), { um:ReadString(), um:ReadShort() } )
end
usermessage.Hook( "_pcmod_beamdata_int", pc_beamdata_int )

// ---------------------------------------------------------------------------------------------------------------------------------
// pc_beamdata_float: Usermessage Hook: Pushes data through to the program
// ---------------------------------------------------------------------------------------------------------------------------------
function pc_beamdata_float( um )
	if (!C_PRO) then C_PRO = {} end
	if (!C_PRO.Exists) then return end
	C_PRO:DataRecieved( PTC("window"), { um:ReadString(), um:ReadFloat() } )
end
usermessage.Hook( "_pcmod_beamdata_float", pc_beamdata_float )

// ---------------------------------------------------------------------------------------------------------------------------------
// Load all custom derma components
// ---------------------------------------------------------------------------------------------------------------------------------
for k, v in pairs( file.FindInLua( "cvgui/*.lua" ) ) do
	include( "cvgui/" .. v )
end

Msg( "== PCMod Clientside OS Loaded ==\n" )